<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="customerEmail">
        <el-input
          v-model="queryParams.customerEmail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="customerName" >
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="customerAge">
        <el-input
          v-model="queryParams.customerAge"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="customerSex" >
        <el-select v-model="queryParams.customerSex" placeholder="请选择性别" clearable style="width: 120px">
          <el-option
            v-for="dict in sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职业" prop="customerProfession" >
        <el-input
          v-model="queryParams.customerProfession"
          placeholder="请输入职业"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="customerAddr">
        <el-input
          v-model="queryParams.customerAddr"
          placeholder="请输入家庭住址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="customerStatus">
        <el-select v-model="queryParams.customerStatus" placeholder="请选择状态" clearable style="width: 140px">
          <el-option
            v-for="dict in customer_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCustomerCreateAt"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCustomerUpdateAt"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['web:customer:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="Edit"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['web:customer:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['web:customer:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['web:customer:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="id" align="center" prop="customerId" />
      <el-table-column label="邮箱" align="center" prop="customerEmail" width="200"/>
      <el-table-column label="电话" align="center" prop="customerPhone" width="200"/>
      <el-table-column label="昵称" align="center" prop="customerName" width="100"/>
      <el-table-column label="头像" align="center" prop="customerAvatar" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.customerAvatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="customerAge" />
      <el-table-column label="性别" align="center" prop="customerSex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.customerSex"/>
        </template>
      </el-table-column>
      <el-table-column label="职业" align="center" prop="customerProfession" width="100"/>
      <el-table-column label="家庭住址" align="center" prop="customerAddr" width="200"/>
      <el-table-column label="状态" align="center" prop="customerStatus">
        <template #default="scope">
          <dict-tag :options="customer_status" :value="scope.row.customerStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="customerCreateAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.customerCreateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="customerUpdateAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.customerUpdateAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" >
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['web:customer:edit']">修改</el-button>-->
          <el-button v-if="scope.row.customerStatus==0" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['web:customer:remove']">删除</el-button>
          <el-button v-if="scope.row.customerStatus==0" link type="primary" icon="Delete" @click="handleLaHei(scope.row)" >拉黑</el-button>
          <el-button v-if="scope.row.customerStatus>0" link type="primary" icon="Delete" @click="handleReset(scope.row)" >恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="customerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="邮箱" prop="customerEmail">
          <el-input v-model="form.customerEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="昵称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="customerAvatar">
          <image-upload v-model="form.customerAvatar"/>
        </el-form-item>
        <el-form-item label="年龄" prop="customerAge">
          <el-input v-model="form.customerAge" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="customerSex">
          <el-select v-model="form.customerSex" placeholder="请选择性别">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职业" prop="customerProfession">
          <el-input v-model="form.customerProfession" placeholder="请输入职业" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="customerAddr">
          <el-input v-model="form.customerAddr" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="状态" prop="customerStatus">
          <el-select v-model="form.customerStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in customer_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Customer">
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer,reCustomer ,LaHeiCustomer} from "@/api/web/customer";

const { proxy } = getCurrentInstance();
const { sys_user_sex, customer_status } = proxy.useDict('sys_user_sex', 'customer_status');

const customerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCustomerCreateAt = ref([]);
const daterangeCustomerUpdateAt = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerId: null,
    customerEmail: null,
    customerPhone: null,
    customerName: null,
    customerAge: null,
    customerSex: null,
    customerProfession: null,
    customerAddr: null,
    customerStatus: null,
    customerCreateAt: null,
    customerUpdateAt: null
  },
  rules: {
    customerPhone: [
      { required: true, message: "电话不能为空", trigger: "blur" },
      { pattern: /^1[3456789]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" }
    ],
    customerPwd: [
      { required: true, message: "密码不能为空", trigger: "blur" }
    ],
    customerName: [
      { required: true, message: "昵称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户信息列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCustomerCreateAt && '' != daterangeCustomerCreateAt) {
    queryParams.value.params["beginCustomerCreateAt"] = daterangeCustomerCreateAt.value[0];
    queryParams.value.params["endCustomerCreateAt"] = daterangeCustomerCreateAt.value[1];
  }
  if (null != daterangeCustomerUpdateAt && '' != daterangeCustomerUpdateAt) {
    queryParams.value.params["beginCustomerUpdateAt"] = daterangeCustomerUpdateAt.value[0];
    queryParams.value.params["endCustomerUpdateAt"] = daterangeCustomerUpdateAt.value[1];
  }
  listCustomer(queryParams.value).then(response => {
    customerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    customerId: null,
    customerEmail: null,
    customerPhone: null,
    customerPwd: null,
    customerName: null,
    customerAvatar: null,
    customerAge: null,
    customerSex: null,
    customerProfession: null,
    customerAddr: null,
    customerStatus: null,
    customerCreateAt: null,
    customerUpdateAt: null
  };
  proxy.resetForm("customerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCustomerCreateAt.value = [];
  daterangeCustomerUpdateAt.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.customerId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _customerId = row.customerId || ids.value
  getCustomer(_customerId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改顾客信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["customerRef"].validate(valid => {
    if (valid) {
        addCustomer(form.value).then(response => {
          console.log("新增用户信息")
          console.log(form.value)
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _customerIds = row.customerId || ids.value;
  proxy.$modal.confirm('是否确认删除客户信息编号为"' + _customerIds + '"的数据项？').then(function() {
    return delCustomer(_customerIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 恢复按钮操作 */
function handleReset(row) {
  const _customerIds = row.customerId || ids.value;
  proxy.$modal.confirm('是否确认恢复客户信息编号为"' + _customerIds + '"的数据项？').then(function() {
    return reCustomer(_customerIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("恢复成功");
  }).catch(() => {});
}
/** 拉黑按钮操作 */
function handleLaHei(row) {
  const _customerIds = row.customerId || ids.value;
  proxy.$modal.confirm('是否确认拉黑客户信息编号为"' + _customerIds + '"的数据项？').then(function() {
    return LaHeiCustomer(_customerIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("拉黑成功");
  }).catch(() => {});
}




// /** 导出按钮操作 */
// function handleExport() {
//   proxy.download('web/customer/export', {
//     ...queryParams.value
//   }, `customer_${new Date().getTime()}.xlsx`)
// }

getList();
</script>
